import EventCenter from "../../TFrame/script/helper/EventCenter";
import EventNames from "../EventNames";
import { ActionType } from "../net/Enum";
import ServiceLogic from "../net/ServerLogic";
import Util from "../../../StartAb/Util";
import GardenSystem from "./GardenSystem";
import WXUtil from "../../../StartAb/WXUtil";
import ResLoadingAdapter from "../../../StartAb/ResLoadingAdapter";
import WXRealLogUtil from "../../../StartAb/WXRealLogUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GoGardenButton extends cc.Component {

    @property(cc.Label)
    sunProgressLabel: cc.Label = null;

    @property(cc.Node)
    redPoint: cc.Node = null;

    start() {
        this.redPoint.active = false
        this.updateText();

        EventCenter.on(EventNames.UpdateSunshineButton, this.updateText, this);
    }

    updateText() {
        let waitPlant = GardenSystem.getCurrentWaitFlowerIdx();
        if (waitPlant == -1) {
            this.sunProgressLabel.node.parent.active = false;
        } else {
            let needSun = GardenSystem.getPlantSunshineCnt(waitPlant);
            if (GardenSystem.yourSunshine >= needSun) {
                //展示当前太阳及红点
                this.redPoint.active = true
                this.sunProgressLabel.string = GardenSystem.yourSunshine.toString();
            } else {
                this.sunProgressLabel.string = GardenSystem.yourSunshine + "/" + needSun;
            }
        }
    }

    _loadGardenRes() {
        ResLoadingAdapter.show({
            title: ''
        })
        cc.assetManager.loadBundle("garden", (err, bundle) => {
            if (err) {
                ResLoadingAdapter.hide()
                WXUtil.retryCall(this._loadGardenRes, this, "提示")
                WXRealLogUtil.error(`加载资源出错:${err.message}`)
                return console.error(err.message)
            }
            cc.director.loadScene("Garden", (err: Error, scene: cc.Scene) => {
                ResLoadingAdapter.hide()
                Util.changeScene("Garden")
            })
        })
    }

    click(e: cc.Event.EventTouch) {
        Util.disableNodeButton(e.target);
        // ServiceLogic.action(ActionType.ActionEnterGarden, { "beiz": cc.director.getScene().name })

        this._loadGardenRes();

        WXRealLogUtil.info(`进入花园`)
    }

}
